﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.AppInfo.controls
{
    public partial class upload : UserControl
    {
        public upload()
        {
            InitializeComponent();
        }

        private JuCloudy.Framework.Interface.IStructure Structure =
                    (JuCloudy.Framework.Interface.IStructure)Application.Current.RootVisual;

        public JuCloudy.Data.Entity.SysAppInfo AppInfo { get; set; }

        private byte[] FileBuffer { get; set; }

        public void DoUpload()
        {
            this.mask_loading.Visibility = Visibility.Visible;
            UploadService.WSUploadServiceSoapClient client =
                new UploadService.WSUploadServiceSoapClient();
            client.ProcessRequestAsync(AppInfo.AppId, this.txt_diary.Text, AppInfo.AppAssembly, FileBuffer.Length, FileBuffer);
            client.ProcessRequestCompleted += (obj, ex) =>
            {
                this.mask_loading.Visibility = Visibility.Collapsed;
                if (ex.Error != null)
                    throw ex.Error;

                Structure.CloseChildDialog();
            };
        }

        private void btn_selectfile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Filter = "Silverlight Application Package File (*.xap)|*.xap";
            if (dialog.ShowDialog() == true)
            {
                this.txt_filename.Text = dialog.File.Name;
                
                try
                {
                    this.txt_filename.Text=dialog.File.FullName;
                }
                catch { }

                using (var stream = dialog.File.OpenRead())
                {
                    byte[] bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    // 设置当前流的位置为流的开始
                    stream.Seek(0, System.IO.SeekOrigin.Begin);
                    this.FileBuffer = bytes;
                    stream.Close();
                }
            }
        }

        private void btn_submit_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_diary.Text))
                this.txt_diary.Text = "没有更新日志。";

            if (String.IsNullOrEmpty(this.txt_filename.Text))
            {
                MessageBox.Show("必须选择应用程序包！");
                return;
            }
            else if (this.FileBuffer == null)
            {
                MessageBox.Show("指定文件不存在或没有读取权限！");
                return;
            }
            else
            {
                this.DoUpload();
            }
        }
    }
}
